package com.hrc.springbootdemo.mq.rabbitmq.service;

import com.hrc.springbootdemo.mq.rabbitmq.config.RabbitConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MessageReceiver {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /** 消息监听器 自动消费*/
    @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        log.info("Received message: {}" ,message);
    }

    /** 手动消费*/
    public Object receiveMessageManually() {
        return rabbitTemplate.receiveAndConvert(RabbitConfig.QUEUE_NAME);
    }

}